//
//  DocumentFileTableViewCell.m
//  OAProject
//
//  Created by yhj on 2017/4/12.
//  Copyright © 2017年 cdnunion. All rights reserved.
//

#import "DocumentFileTableViewCell.h"
#import "NSString+SizeFile.h"

@interface DocumentFileTableViewCell ()

// 图片
@property(nonatomic,strong)UIImageView *fileImage;

@property(nonatomic,strong)UIButton *isSelectBtn;

@property(nonatomic,strong)UILabel *nameLabel;

@property(nonatomic,strong)UILabel *sizeLabel;

@end


@implementation DocumentFileTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        // ➡️Cell类型
        self.accessoryType=UITableViewCellAccessoryNone;

        self.fileImage.hidden=NO;

        self.isSelectBtn.hidden=NO;

        self.nameLabel.font=Font_Number(14);

        self.sizeLabel.font=Font_Number(12);
        
        self.backgroundColor=WhiteColor;
    }
    return self;
}

-(UIButton *)isSelectBtn
{
    if (!_isSelectBtn) {
        _isSelectBtn=[AppUI createButtonWithSupeView:self.contentView andWithNormalImage:[UIImage imageNamed:@"no_select"] andWithSelectImage:[UIImage imageNamed:@"selected"] andWithBtnAction:^{

            [self isSelectDocumentFileBtnEvent:self.isSelectBtn];

        }];
        [self.contentView addSubview:self.isSelectBtn];
        [self.isSelectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(kmargin*2);
            make.size.mas_equalTo(CGSizeMake(kmargin*2,kmargin*2));
            make.centerY.mas_equalTo(0);
        }];
    }
    return _isSelectBtn;
}


// fileImage
-(UIImageView *)fileImage
{
    if (!_fileImage) {
        _fileImage=[UIImageView new];
        [self.contentView addSubview:_fileImage];
        [_fileImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.isSelectBtn.mas_right).offset(kmargin*2);
            make.top.mas_equalTo(kmargin);
            make.bottom.mas_equalTo(-kmargin);
            make.width.mas_equalTo(kmargin*6);
        }];
    }
    return _fileImage;
}

// nameLabel
- (UILabel *)nameLabel
{
    if (!_nameLabel) {
        _nameLabel=[self getLabelWithText:@"文件名字" font:Font_Number(14) textColor:DarkGreyColor textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
        [self.contentView addSubview:_nameLabel];
        [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.fileImage.mas_right).offset(kmargin);
            make.centerY.mas_equalTo(-kmargin);
        }];
    }
    return _nameLabel;
}

// sizeLabel
- (UILabel *)sizeLabel
{
    if (!_sizeLabel) {
        _sizeLabel=[self getLabelWithText:@"文件大小" font:Font_Number(12) textColor:LightGreyColor textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
        [self.contentView addSubview:_sizeLabel];
        [_sizeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.nameLabel.mas_left);
            make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(kmargin);
        }];
    }
    return _sizeLabel;
}

-(void)configDocumentFileWithFilePath:(NSString *)filePath
{
    self.filePath=filePath;
    self.fileImage.image=[UIImage imageNamed:filePath];
    self.nameLabel.text=[NSURL fileURLWithPath:filePath].lastPathComponent;
    self.sizeLabel.text=[Util transformedValue:@(filePath.fileSize)];
}

-(void)isSelectDocumentFileBtnEvent:(UIButton *)sender
{
    sender.selected=!sender.selected;
   if (self.delegate && [self.delegate respondsToSelector:@selector(selectDocumentFileWithFilePath:isSelect:)])
    {
        [self.delegate selectDocumentFileWithFilePath:self.filePath isSelect:sender.selected];
    }
}

@end
